﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Xajhzwb.Components.Helper;
using System.IO;
using System.Xml;

namespace Xajhzwb.Components.Util
{
    public class Language
    {
        public static string LanguagePath = Utils.StartupPath + "\\Language";

        private static string GetLanguageFile()
        {
            DataTable languageTable = GetLanguageList();

            Config config = new Config();
            DataRow[] rows = languageTable.Select("LANGUAGE_NAME='" + config.Language + "'");
            string languageFile = rows[0]["LANGUAGE_FILE"].ToString();

            return Path.Combine(LanguagePath, languageFile);
        }

        #region Get Language Text
        public static DataTable GetLanguageList()
        {
            DataTable languageTable = new DataTable();
            languageTable.Columns.Add("LANGUAGE_NAME");
            languageTable.Columns.Add("LANGUAGE_FILE");

            string languageFolder = Utils.StartupPath + "\\Language";

            List<FileInfo> languageFileList = FileHelper.GetFiles(languageFolder, "*.xml", SearchOption.TopDirectoryOnly);
            if(languageFileList != null && languageFileList.Count > 0)
            {
                foreach(FileInfo languageFile in languageFileList)
                {
                    XmlDocument languageDoc = new XmlDocument();
                    languageDoc.Load(languageFile.FullName);

                    XmlNode languageNode = languageDoc.SelectSingleNode("QidorSprider/Language");

                    DataRow newRow = languageTable.NewRow();
                    newRow["LANGUAGE_NAME"] = languageNode.Attributes["name"].Value;
                    newRow["LANGUAGE_FILE"] = languageNode.Attributes["filename"].Value;
                    languageTable.Rows.Add(newRow);
                }
            }

            return languageTable;
        }


        public static string GetMainText(string itemID)
        {
            return GetLanguageText("Main", itemID);
        }

        public static string GetMessageText(string itemID)
        {
            return GetLanguageText("Message", itemID);
        }

        public static string GetLanguageText(string nodeType, string itemID)
        {
            string languageFile = GetLanguageFile();
            string languageText = string.Empty;

            if(!File.Exists(languageFile))
            {
                throw new Exception("language file is not exist!");
            }
            else
            {
                XmlDocument textListDoc = new XmlDocument();
                textListDoc.Load(languageFile);

                XmlNodeList textNodeList = textListDoc.SelectNodes(string.Format("QidorSprider/Language/{0}/Item", nodeType));
                if(textNodeList != null && textNodeList.Count > 0)
                {
                    foreach(XmlNode textNode in textNodeList)
                    {
                        if(textNode.Attributes["id"].Value == itemID)
                        {
                            languageText = textNode.Attributes["name"].Value;
                        }
                    }
                }
            }

            return languageText;
        }
        #endregion

    }
}
